require(["../../conf/config.js"],function(){
	
	//购物车页面的跳转
	require(["jquery"],function($){
		//点击跳转主页
		$("#backHome").click(function(){
			//alert()
			window.location.href="/home";
		})
		$("#add-item").click(function(){
			window.location.href="/pages/index/airClear.html";
		})
		$("#loginBtn").click(function(){
			window.location.href="/pages/index/login.html";
		})
	})
	
	//用户已登陆的头部信息
	require(["getCookie","jquery","setCookie"],function(getCookie,$,setCookie){
		//读取是否存在cookie
		let isCookie = document.cookie.indexOf("username");
		if(isCookie != -1){
			//读取cookie
			let name = getCookie("username");
			let pwd = getCookie("userpass");
			
			//将"登录"改为“用户名”
			$("#loginBtn").text(name[1]);
			//添加“我的订单”
			$("#loginBtn").parent().append("<span>我的订单</span>")
			
			//划过用户名
			$("#loginBtn").hover(function(){
				$(".user-msg").slideDown("slow");
			},function(){
				let time = setTimeout(function(){
					//$(".user-msg").addClass("navHide");
					$(".user-msg").hide();
				},500);
				
				//消息块的显示
				$(".user-msg").hover(function(){
					clearTimeout(time)
					$(".user-msg").removeClass("navHide");
				},function(){
					//$(".user-msg").addClass("navHide");
					$(".user-msg").hide();
				});
			})
			
			//退出登录
			$("#log-out").click(function(){
				//让cookie过期
				setCookie("username",name[1],-1);
				setCookie("userpass",pwd[1],-1);
				
				alert("退出成功");
				$("#loginBtn").css("width","2em");
				$("#loginBtn").nextAll().show();
				$("#loginBtn").text("登录");
				$("#loginBtn").next().remove();
			})
		}
	})
	
	//购物车的数据填充
	require(["jquery","shoppingData","animate"],function($,buyCarData,animate){
		//获取推荐商品的信息
		buyCarData();
		
		//console.log(document.getElementById("hot-items").children)
		let hotItem = document.getElementById("hot-items").children;
		setInterval(function(){
			for(let i = 0;i<hotItem.length;i++){
				for(let j = 0;j<hotItem.length;j++){
					hotItem[j].style.display = "none";
				}
				hotItem[i].style.display = "block";
			}
		},1000)
		/*Array.from($("#hot-item")).forEach(function(item){
				animate(item,{opacity:0})
		})
		
		let currentIndex = 0;
		let ullist = Array.from(document.getElementById("hot-item").children);
		console.log(ullist);
		setInterval(function(){
			currentIndex = ++currentIndex == ullist.length?0:currentIndex;
			animate(ullist[currentIndex],{opacity:10});
			
		},500)*/
		//mySwiper("#ooo",{left:true});
		//setInterval(function(){
			/*let currentIndex = 0;*/
			
			
		//},2000);
		
	})
	
	//购物车添加商品
	require(["jquery","getCookie"],function($,getCookie){
		//找到table
		let _table = document.getElementsByTagName("table")[1].children[0];
		let _empty = document.getElementsByClassName("emptyCar")[0];
		console.log(_table)
		//console.log(1111)
		
		//读取shoppingList
		let goodsList = JSON.parse(getCookie("shoppingCar")[1]);
		//console.log(JSON.parse(getCookie("shoppingCar")[1]);
		if(goodsList == ""){
			_empty.className = "emptyCar";
		}else{
			_empty.className = "emptyCar navHide";
			
			//向页面添加商品
			goodsList.forEach(function(item){
				_table.innerHTML += `
					<tr>
						<td width="57" ><input type="checkbox" checked="checked"/></td>
						<td width="148"><a><img src="${item._img}"/></a></td>
						<td width="257">${item._name}</td>
						<td width="163"></td>
						<td width="152">${item._price}</td>
						<td width="152">
							<label class="count_box">
								<input type="button" value="-"/>
								<input type="text" value="${item._count}"/>
								<input type="button" value="+"/>
							</label>
						</td>
						<td width="152" class="item_pri" style="color:red">${item._count*item._price}</td>
						<td width="10%"><span class="delete-item">删除</span><span>移入收藏夹</span></td>
					</tr>
				`;
			})
		
		}
	})
	
	//购物车增删改
	require(["jquery","getCookie","setCookie"],function($,getCookie,setCookie){
		//读取shoppingList
		let goodsList = JSON.parse(getCookie("shoppingCar")[1]);
		
		if(goodsList != ""){
			//对商品的删除,收藏操作
			let _trs = Array.from(document.getElementsByTagName("table")[1].children[0].children);
			console.log(_trs)	
			
			_trs.forEach(function(trs){
				trs.addEventListener("click",function(e){
					
					//获取要操作的商品名称
					let itemName = this.children[2].innerText;
					
					//删除
					//判断点击的是哪一个按钮
					if(e.target.innerText == "删除"){  //如果点击的按钮是删除
						
						//goodsList数组中删除该商品
						let _delTrsIndex = 0;
						
						goodsList.forEach(function(goods,index){
							if(goods._name == itemName){
								_delTrsIndex = index; //记录下要删除商品的下标
							}
						})
						
						goodsList.splice(_delTrsIndex,1); //从要删除的下标开始删除
						setCookie("shoppingCar",JSON.stringify(goodsList),10);
						console.log(getCookie("shoppingCar"));
						this.remove();
						location.reload()
					}
					
					
					//修改商品的数量--
					if(e.target.value == "-"){
						console.log(e.target)
						goodsList.forEach(function(goods,index){
							if(goods._name == itemName){
								goods._count--;
								if(goods._count<1){
									goods._count = 1;
								}
								//修改数量
								e.target.nextElementSibling.value = goods._count;
								e.target.parentNode.parentNode.nextElementSibling.innerText = goods._count * goods._price
								
							}
						})
						console.log(e.target.nextElementSibling.value)
						setCookie("shoppingCar",JSON.stringify(goodsList),10);
					}
					//修改商品的数量++
					if(e.target.value == "+"){
						console.log(e.target)
						
						goodsList.forEach(function(goods,index){
							if(goods._name == itemName){
								goods._count++;
								//修改数量
								//console.log(e.target.parentNode.parentNode.nextElementSibling.innerText)
								e.target.previousElementSibling.value = goods._count;
								e.target.parentNode.parentNode.nextElementSibling.innerText = goods._count * goods._price
							}
						})
						console.log(e.target.previousElementSibling.value)
						setCookie("shoppingCar",JSON.stringify(goodsList),10);
					}
					
				})
			})
		}
	})

	//购物车结算
	require(["getCookie","jquery"],function(getCookie,$){
		let total = document.getElementsByClassName("item-total")[0];
		let pay_off = document.getElementsByClassName("pay-off")[0];
		
		//获取所有的价格标签
		let totalPrice = Array.from(document.getElementsByClassName("item_pri"));
		console.log(totalPrice)
		
		let money = 0;
		let discount = 0;
		totalPrice.forEach(function(price){
			money += Number(price.innerText);
		})
		
		if(JSON.parse(getCookie("shoppingCar")[1])!=""){
			total.innerHTML = `
				<div class="item-total-left">
					<div><label><input type="checkbox" id="all-checked"/>全选</label></div>
					<p>删除选中商品</p>
				</div>
				<ul class="item-total-right">
					<li id="good-money">商品总价：${money}</li>
					<li>优惠节省：${discount}</li>
					<li id="result-money">合计：<span style="color: red;">${money-discount}</span></li>
				</ul>
			`;
			total.className = "item-total";
			pay_off.className = "pay-off";
		}else{
			total.className = "item-total navHide";
			pay_off.className = "pay-off navHide";
		}
		
		//购物车全选
		$("#all-checked").click(function(){
			//全选
			if($("#all-checked").prop("checked")){
				$("input[type='checkbox']").prop("checked","true"); 
			}else{
				//全不选
				$("input[type='checkbox']").removeAttr("checked"); 
			}
		})
		
		
	})



})